<?php
namespace app\product\model;
use app\common\library\Model;

class ProductOrder extends Model
{
    protected $append = [
        'stage_name','status_name','customer_mobile'
    ];

    public function getStageNameAttr($value, $data) {
        $_stage = config('product.product_order_field_stage');
        return $_stage[$data['stage']];
    }

    public function getStatusNameAttr($value, $data) {
        $_status = config('product.product_order_field_status');
        return $_status[$data['status']];
    }

    public function getCustomerMobileAttr($value, $data) {
        if($data['stage'] > -1 && $data['customer_id']) {
            return model('user/user')->where(['id' => $data['customer_id']])->value("mobile");
        }
        return false;
    }

    public function product() {
        return $this->hasOne("app\\product\\model\\Product", "id", "product_id");
    }

    public function user() {
        return $this->hasOne("app\\user\\model\\User", 'id', 'uid');
    }

    public function CustomerInfo() {
        return $this->hasOne("app\\user\\model\\User", 'id', 'customer_id');
    }

    /**
     * 获取初核问卷模板
     * @param $value
     * @param $data
     * @return array|bool|false|\PDOStatement|string|\think\Model
     */
    public function getQuestionTemplateInfoAttr($value, $data) {
        $question_template_id = $data['question_template_id'];
        if($question_template_id) {
            return model('product/question_template')->find($question_template_id);
        }
        return false;
    }

    /**
     * 获取资料模板内容
     * @param string $value
     * @param array $data
     * @return array
     */
    public function getProfileTemplateInfoAttr($value, $data = []) {
        $profile_template_id = $data['profile_template_id'];
        if($profile_template_id) {
            return model('product/profile_template')->find($profile_template_id);
        }
        return false;
    }

    /**
     * 查询订单的进度追踪
     */
    public function OrderTrace() {
        return $this->hasMany("app\\product\\model\\ProductOrderTrace", 'order_sn', "sn");
    }
}